import request from '@/utils/api'

export const getRecentPublicNotesWithStats = async (limit = 10, currentUserId = null) => {
  const res = await request.get('/api/notes/with-stats', {
    params: { limit, userId: currentUserId }
  })
  
  if (res.code !== 200) throw new Error(res.msg || '获取公开笔记失败')
  return res 
}

export const getPublicNotesByTag = async (tagId, limit = 10, currentUserId = null) => {
  const res = await request.get('/api/notes/public-by-tag', {
    params: { tagId, limit, userId: currentUserId }
  })
  if (res.code !== 200) throw new Error(res.msg || '按标签获取笔记失败')
  return res.data || []
}

export const getNoteById = async (id) => {
  const res = await request.get(`/api/notes/${id}`)
  if (res.code !== 200) throw new Error(res.msg || '获取笔记详情失败')
  return res.data || null
}

export const createNote = async (noteData) => {
  try {
    const response = await request.post('/api/notes', noteData)
    return {
      data: response.data || null,
      code: response.code || 200,
      msg: response.msg || '创建成功'
    }
  } catch (error) {
    console.error('创建笔记失败:', error)
    return {
      data: null,
      code: error.response?.status || 500,
      msg: error.response?.data?.msg || error.message || '创建失败'
    }
  }
}

export const updateNote = async (id, noteData) => {
  try {
    const response = await request.put(`/api/notes/${id}`, noteData)
    return {
      data: response.data || null,
      code: response.code || 200,
      msg: response.msg || '更新成功'
    }
  } catch (error) {
    console.error('更新笔记失败:', error)
    return {
      data: null,
      code: error.response?.status || 500,
      msg: error.response?.data?.msg || error.message || '更新失败'
    }
  }
}

export const deleteNote = async (id) => {
  try {
    const response = await request.delete(`/api/notes/${id}`)
    return {
      data: response.data || null,
      code: response.code || 200,
      msg: response.msg || '删除成功'
    }
  } catch (error) {
    console.error('删除笔记失败:', error)
    return {
      data: null,
      code: error.response?.status || 500,
      msg: error.response?.data?.msg || error.message || '删除失败'
    }
  }
}

export const getLikedNotesWithStats = async (userId) => {
  try {
    const response = await request.get(`/api/notes/liked-by/${userId}`)
    return {
      data: response.data || [],
      code: response.code || 200,
      msg: response.msg || '操作成功'
    }
  } catch (error) {
    console.error('获取点赞笔记失败:', error)
    return {
      data: [],
      code: error.response?.status || 500,
      msg: error.response?.data?.msg || error.message || '获取失败'
    }
  }
}

export const getCollectedNotesWithStats = async (userId) => {
  try {
    const response = await request.get(`/api/notes/collected-by/${userId}`)
    return {
      data: response.data || [],
      code: response.code || 200,
      msg: response.msg || '操作成功'
    }
  } catch (error) {
    console.error('获取收藏笔记失败:', error)
    return {
      data: [],
      code: error.response?.status || 500,
      msg: error.response?.data?.msg || error.message || '获取失败'
    }
  }
}

export const getUserNotesWithStats = async (userId, currentUserId) => {
  try {
    const response = await request.get(`/api/notes/user/${userId}/with-stats`, {
      params: { 
        currentUserId 
      }
    })
    return {
      data: response.data || [],
      code: response.code || 200,
      msg: response.msg || '操作成功'
    }
  } catch (error) {
    console.error('获取用户笔记失败:', error)
    return {
      data: [],
      code: error.response?.status || 500,
      msg: error.response?.data?.msg || error.message || '获取失败'
    }
  }
}

export const uploadMedia = async (file) => {
  try {
    const formData = new FormData()
    formData.append('file', file)
    
    const response = await request.post('/api/media/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    
    if (response.code !== 200) {
      throw new Error(response.msg || '上传失败')
    }
    
    return {
      url: response.data.url,
      mediaId: response.data.mediaId
    }
  } catch (error) {
    console.error('上传媒体失败:', error)
    throw error
  }
}